import java.util.Arrays;

public class SeqList {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_CAPACITY = 5;

    public SeqList() {
        this.elem = new int[DEFAULT_CAPACITY];
    }

    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    public void add(int data) {
        if (isFull()) {
            resize();
        }
        this.elem[usedSize] = data;
        usedSize++;
    }

    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (toFind == elem[i]) {
                return true;
            }
        }
        return false;
    }

    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    public int get(int pos) {
        if (!checkPos(pos)) {
            throw new PosOutOfBoundsExcption("get获取元素时，元素下标不合法");
        }
        return elem[pos];
    }

    private boolean isFull() {
        return usedSize == elem.length ? true : false;
    }

    private void resize() {
        elem = Arrays.copyOf(elem, elem.length * 2);
    }

    private boolean checkPos(int pos) {
        if (pos < 0 || pos >= usedSize) {
            return false;
        }
        return true;
    }
    private boolean isEmpty(){
        return usedSize == 0;
    }

    public void add(int pos, int data){
        if(pos < 0 || pos > this.usedSize){
            throw new PosOutOfBoundsExcption("add元素时 下标不合法");
        }
        if (isFull()){
            resize();
        }
        for (int i = this.usedSize - 1; i >= pos ; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }
    public void remove(int toRemove){
        if(isEmpty()){
            return;
        }
        int index = indexOf(toRemove);
        if(index == -1){
            return;
        }
        for (int i = index; i < usedSize; i++) {
            this.elem[i] = this.elem[i+1];
        }
        usedSize --;
        //如果为引用类型
        //elem[usedSize] = null;
    }
    public void clear(){
        //如果为引用类型
//        for (int i = 0; i < usedSize; i++) {
//            this.elem[i] = null;
//        }
        usedSize = 0;
    }
}
